/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;
import  java.util.ArrayList;
import  java.util.StringTokenizer;
import  java.io.IOException;
import  java.io.BufferedReader;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.PrintWriter;

import  com.evolve.webbot.bean.Sanbrns;
/**
 *
 * @author DarthAE
 */
public class TxtSanborns 
{
    private String filePath;

    public TxtSanborns(){}
    
    public TxtSanborns(String filePath)
    {
        this.filePath=filePath;
    }
    
    public ArrayList<Sanbrns>  readSanbornsTxtFile()
    {
      ArrayList<Sanbrns> sanbornsRecordList= new ArrayList<Sanbrns>();  
      boolean inicioCant = false;  
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(this.filePath));
             String linea = br.readLine();

             while (linea != null) 
             {

               if(linea.contains("Total"))
               {
                   inicioCant = false;
               }    
               
               if(inicioCant)
               { 
                 if(linea.compareToIgnoreCase("unchecked")!=0)
                 {
                     /*System.out.println(linea.trim());*/                    
                     sanbornsRecordList.add(getValues(linea.trim()));
                 }                 
               }               
               
               if(linea.contains("Departamento"))
               {
                    inicioCant = true; 
               }   
               
               linea = br.readLine();
             }
             
           } catch(IOException e){}
      
      return sanbornsRecordList;
    }
    
    public void writeSanbornsDataFile(ArrayList<Sanbrns> sanbornsRecordList)
    {
        try {
               FileWriter  outFile = new FileWriter(this.filePath.replaceAll("vyc","data"));
               PrintWriter out     = new PrintWriter(outFile);
              
               for(int i=0;i<sanbornsRecordList.size();i++)
               {
                 out.println(sanbornsRecordList.get(i).toString());
               }
              
               out.close();
               
            } catch (IOException e){e.printStackTrace();}
    }
    
    public Sanbrns getValues(String linea)
    {
        Sanbrns sanborns = new Sanbrns();
        boolean  esDesc   = false;
        String  value     = "";
        String  descValue = "";
        int     k         =  0;
        int     l         =  0;
        
        StringTokenizer tokens = new StringTokenizer(linea);

        while(tokens.hasMoreTokens())
        {
          value=tokens.nextToken().trim();
          System.out.println("->"+value);
          
          if(k==0)
          {
              sanborns.setEan(value);
          }
          if(k==1)
          {
              sanborns.setSku(value);
          }
          else 
          if(k>1 && !esDesc)
          {
             if(value.length()< 2)
             {
                 descValue = descValue + value +" ";
             }
             else
             if(value.length()>2 &&(!value.contains("0021")))
             {
                 descValue = descValue+value+" ";
             }
             else
             if(value.length()>2 &&(value.contains("0021")))
             {
                 sanborns.setDescripcion(descValue.trim());
                 sanborns.setDepartamento(value+" "+tokens.nextToken());
                 descValue = "";
                 esDesc    = true;
             }
                 
          }
          if(k>0 && esDesc)
          {
              /*if(l==0)
              {
                 tokens.nextToken(); 
              }*/
              
              switch(l)
              {
                  case 1:sanborns.setVtaPza(value);
                         break;
                  case 2:sanborns.setVtaCosto(value);
                         break;
                  case 3:sanborns.setVtaPrecio(value);
                         break;
                  case 4:sanborns.setComprasPza(value);
                         break;
                  case 5:sanborns.setExistenciaPza(value);
                         break;
                  case 6:sanborns.setExistencia(value);
                         break;
                  case 7:sanborns.setDiasInv(value);
                         break;
                  default:System.out.println("null value");
                          break;
                      
              }
           
              l++;
          }
          
          k++;
        }            
        
        System.out.println(sanborns);
        return sanborns;
    }    
    
//    public static void main(String args[])
//    {
//        ArrayList<Sanbrns> sanbornsRecordList = new ArrayList<Sanbrns>();
//        TxtSanborns txtSanborns                = new TxtSanborns("");
//        
//        sanbornsRecordList = txtSanborns.readSanbornsTxtFile();
//        txtSanborns.writeSanbornsDataFile(sanbornsRecordList);
//    }    
}
